#include "widget.h"
#include <QApplication>
#include <QContextMenuEvent>
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QTimer>
#include <QPainter>

// 温度曲线相关的宏
#define INCREMENT     3   // 温度每升高/降低1度，y轴坐标的增量
#define POINT_RADIUS  3   // 曲线描点的大小
#define TEXT_OFFSET_X 12  // 温度文本相对于点的偏移
#define TEXT_OFFSET_Y 10  // 温度文本相对于点的偏移

// 重写父类的虚函数
// 父类中默认的实现是忽略右键菜单事件
// 重写之后，处理右键菜单事件
void Widget::contextMenuEvent(QContextMenuEvent *event)
{

    mExitMenu->exec(QCursor::pos());
    //调用accept表示  这个事件我已经处理,不需要向上传递了
    event->accept();
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "窗口左上角：" << this->pos() << ", 鼠标坐标点：" << event->globalPos();
    mOffset = event->globalPos() - this->pos();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - mOffset);
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == lblHigh && event->type() == QEvent::Paint)
    {
        paintHighCurve();
    }

    if(watched == lblLow && event->type() == QEvent::Paint)
    {
        paintLowCurve();
    }

    return QWidget::eventFilter(watched, event);

}

void Widget::initTop()
{
    //1 城市输入框
    QLineEdit* leCity = new QLineEdit(this);
    leCity->setFixedWidth(360);
    leCity->setStyleSheet(R"(
                          font: 14pt Microsoft YaHei;
                          background-color: rgb(255, 255, 255);
                          border-radius: 4px;
                          padding: 4px 8px
                      )");
    //2 搜索按钮
    QPushButton* btnSearch = new QPushButton(this);

    btnSearch->setStyleSheet("background-color: rgba(255, 255, 255,0);");
    btnSearch->setIcon(QIcon(":/search.png"));
    btnSearch->setIconSize(QSize(24, 24));

    //3 弹簧
    QSpacerItem* space = new QSpacerItem(32, 32, QSizePolicy::Expanding, QSizePolicy::Minimum);


    //4 日期
    lblDate = new QLabel(this);
    lblDate->setStyleSheet(R"(
                           font: 20pt Arial;
                           background-color: rgba(255, 255, 255,0);
                       )");
    lblDate->setAlignment(Qt::AlignCenter);
    lblDate->setText("2024/01/12 星期五");

    topLayout->addWidget(leCity);
    topLayout->addWidget(btnSearch);
    topLayout->addItem(space);
    topLayout->addWidget(lblDate);


}

void Widget::initLeft()
{
    //1 天气  温度  城市
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setSpacing(0);
    // 1.1 天气类型
    lblTypeIcon = new QLabel(this);
    lblTypeIcon->setFixedSize(150, 150);
    lblTypeIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    lblTypeIcon->setPixmap(QPixmap(":/DuoYun.png"));
    lblTypeIcon->setScaledContents(true);
    lblTypeIcon->setAlignment(Qt::AlignCenter);
    layout->addWidget(lblTypeIcon);

    QVBoxLayout* vLayout = new QVBoxLayout();
    QHBoxLayout* hLayout1 = new QHBoxLayout();
    QHBoxLayout* hLayout2 = new QHBoxLayout();

    // 1.2
    // 温度
    lblTemp = new QLabel(this);
    lblTemp->setText("28°");
    lblTemp->setStyleSheet(R"(
           font: 50pt "Arial";
           background-color: rgba(255, 255, 255, 0);
           padding: 0px;
       )");
    lblTemp->setAlignment(Qt::AlignBottom);
    lblTypeIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hLayout1->addWidget(lblTemp);

    // 城市
    lblCity = new QLabel(this);
    lblCity->setText("北京");
    lblCity->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblCity->setAlignment(Qt::AlignTop);
    lblCity->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hLayout2->addWidget(lblCity);

    // 弹簧
    QSpacerItem* item2 = new QSpacerItem(16, 1, QSizePolicy::Fixed, QSizePolicy::Minimum);
    hLayout2->addItem(item2);

    // 类型
    lblType = new QLabel(this);
    lblType->setText("晴转多云");
    lblType->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblType->setAlignment(Qt::AlignTop);
    lblType->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    hLayout2->addWidget(lblType);

    // 弹簧
    QSpacerItem* item3 = new QSpacerItem(16, 1, QSizePolicy::Fixed, QSizePolicy::Minimum);
    hLayout2->addItem(item3);

    // 高低温
    lblLowHigh = new QLabel(this);
    lblLowHigh->setText("19~31°");
    lblLowHigh->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblLowHigh->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    lblLowHigh->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hLayout2->addWidget(lblLowHigh);

    vLayout->addLayout(hLayout1);
    vLayout->addLayout(hLayout2);
    layout->addLayout(vLayout);
    leftLayout->addLayout(layout);

    // 2. 弹簧
    QSpacerItem* spaceItem2 = new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
    leftLayout->addItem(spaceItem2);

    // 3. 感冒指数
    lblGanMao = new QLabel(this);
    lblGanMao->setText("感冒指数：儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼");
    lblGanMao->setStyleSheet(R"(
           background-color: rgba(60, 60, 60, 0);
           padding-left: 5px;
           padding-right: 5px;
           font: 12pt "Microsoft YaHei"
       )");
    lblGanMao->setWordWrap(true);
    leftLayout->addWidget(lblGanMao);

    // 4. 风力/PM2.5/湿度/空气质量
    QWidget* widget = new QWidget(this);
    widget->setStyleSheet(R"(
           background-color: rgb(157, 133, 255);
           border-radius: 15px
       )");
    QGridLayout* gridLayout = new QGridLayout(widget);
    gridLayout->setHorizontalSpacing(40);
    gridLayout->setVerticalSpacing(30);
    gridLayout->setContentsMargins(40, 25, 40, 25);

    // 4.1 风力
    QHBoxLayout* hItem0 = new QHBoxLayout();
    hItem0->setSpacing(0);

    lblFlIcon = new QLabel(this);
    lblFlIcon->setFixedSize(72, 72);
    lblFlIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    lblFlIcon->setPixmap(QPixmap(":/wind.png"));
    lblFlIcon->setScaledContents(true);
    lblFlIcon->setAlignment(Qt::AlignCenter);

    hItem0->addWidget(lblFlIcon);

    QVBoxLayout* vItem0 = new QVBoxLayout();
    lblFx = new QLabel(this);
    lblFx->setText("西北风");
    lblFx->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 10pt "Microsoft YaHei";
           padding: 24px 0px 0px 0px;
       )");
    lblFx->setAlignment(Qt::AlignCenter);
    lblFx->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem0->addWidget(lblFx);

    lblFl = new QLabel(this);
    lblFl->setText("3级");
    lblFl->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: bold 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblFl->setAlignment(Qt::AlignCenter);
    lblFl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem0->addWidget(lblFl);

    hItem0->addLayout(vItem0);
    gridLayout->addLayout(hItem0, 0, 0, 1, 1);

    // 4.2 PM2.5
    QHBoxLayout* hItem1 = new QHBoxLayout();
    hItem1->setSpacing(0);

    lblPM25Icon = new QLabel(this);
    lblPM25Icon->setFixedSize(72, 72);
    lblPM25Icon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    lblPM25Icon->setPixmap(QPixmap(":/pm25.png"));
    lblPM25Icon->setScaledContents(true);
    lblPM25Icon->setAlignment(Qt::AlignCenter);

    hItem1->addWidget(lblPM25Icon);

    QVBoxLayout* vItem1 = new QVBoxLayout();
    lblPM25Title = new QLabel(this);
    lblPM25Title->setText("PM2.5");
    lblPM25Title->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 10pt "Microsoft YaHei";
           padding: 24px 0px 0px 0px;
       )");
    lblPM25Title->setAlignment(Qt::AlignCenter);
    lblPM25Title->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem1->addWidget(lblPM25Title);

    lblPM25 = new QLabel(this);
    lblPM25->setText("10");
    lblPM25->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: bold 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblPM25->setAlignment(Qt::AlignCenter);
    lblPM25->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem1->addWidget(lblPM25);

    hItem1->addLayout(vItem1);
    gridLayout->addLayout(hItem1, 0, 1, 1, 1);

    // 4.3 湿度
    QHBoxLayout* hItem2 = new QHBoxLayout();
    hItem2->setSpacing(0);

    lblHumidityIcon = new QLabel(this);
    lblHumidityIcon->setFixedSize(72, 72);
    lblHumidityIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    lblHumidityIcon->setPixmap(QPixmap(":/humidity.png"));
    lblHumidityIcon->setScaledContents(true);
    lblHumidityIcon->setAlignment(Qt::AlignCenter);

    hItem2->addWidget(lblHumidityIcon);

    QVBoxLayout* vItem2 = new QVBoxLayout();
    lblHumidityTitle = new QLabel(this);
    lblHumidityTitle->setText("湿度");
    lblHumidityTitle->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 10pt "Microsoft YaHei";
           padding: 24px 0px 0px 0px;
       )");
    lblHumidityTitle->setAlignment(Qt::AlignCenter);
    lblHumidityTitle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem2->addWidget(lblHumidityTitle);

    lblHumidity = new QLabel(this);
    lblHumidity->setText("60%");
    lblHumidity->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: bold 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblHumidity->setAlignment(Qt::AlignCenter);
    lblHumidity->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem2->addWidget(lblHumidity);

    hItem2->addLayout(vItem2);
    gridLayout->addLayout(hItem2, 1, 0, 1, 1);

    // 4.4 空气质量
    QHBoxLayout* hItem3 = new QHBoxLayout();
    hItem3->setSpacing(0);

    lblQualityIcon = new QLabel(this);
    lblQualityIcon->setFixedSize(72, 72);
    lblQualityIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    lblQualityIcon->setPixmap(QPixmap(":/aqi.png"));
    lblQualityIcon->setScaledContents(true);
    lblQualityIcon->setAlignment(Qt::AlignCenter);

    hItem3->addWidget(lblQualityIcon);

    QVBoxLayout* vItem3 = new QVBoxLayout();
    lblQualityTitle = new QLabel(this);
    lblQualityTitle->setText("空气质量");
    lblQualityTitle->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: 10pt "Microsoft YaHei";
           padding: 24px 0px 0px 0px;
       )");
    lblQualityTitle->setAlignment(Qt::AlignCenter);
    lblQualityTitle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem3->addWidget(lblQualityTitle);

    lblQuality = new QLabel(this);
    lblQuality->setText("60%");
    lblQuality->setStyleSheet(R"(
           background-color: rgba(255, 255, 255,0);
           font: bold 12pt "Microsoft YaHei";
           padding: 0px 0px 24px 0px;
       )");
    lblQuality->setAlignment(Qt::AlignCenter);
    lblQuality->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vItem3->addWidget(lblQuality);

    hItem3->addLayout(vItem3);
    gridLayout->addLayout(hItem3, 1, 1, 1, 1);

    leftLayout->addWidget(widget);

}

void Widget::initRight()
{
    // 1. 星期/日期
    QGridLayout* gridLayout1 = new QGridLayout();
    gridLayout1->setHorizontalSpacing(6);
    gridLayout1->setVerticalSpacing(0);

    QStringList weekList = {"昨天", "今天", "明天", "周二", "周三", "周四"};
    QStringList dateList = {"12/30", "12/31", "01/01", "01/02", "01/03", "01/04"};

    for ( int i = 0; i < weekList.size(); i++ )
    {
        QLabel* lblWeek = new QLabel(this);
        QLabel* lblDate = new QLabel(this);
        lblWeek->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblDate->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblWeek->setText(weekList[i]);
        lblDate->setText(dateList[i]);
        lblWeek->setStyleSheet(R"(
                border-bottom-left-radius: 0px;
                border-bottom-right-radius: 0px;
                padding-bottom: 2px;
                padding-left:20px;
                padding-right:20px;
                background-color: rgb(10, 180, 190);
            )");
        lblDate->setStyleSheet(R"(
                border-top-left-radius: 0px;
                border-top-right-radius: 0px;
                padding-top: 2px;
                padding-left:20px;
                padding-right:20px;
                background-color: rgb(10, 180, 190);
            )");
        lblWeek->setAlignment(Qt::AlignCenter);
        lblDate->setAlignment(Qt::AlignCenter);

        mWeekList << lblWeek;
        mDateList << lblDate;

        gridLayout1->addWidget(lblWeek, 0, i);
        gridLayout1->addWidget(lblDate, 1, i);
    }

    rightLayout->addLayout(gridLayout1);

    // 2. 天气类型
    QGridLayout* gridLayout2 = new QGridLayout();
    gridLayout2->setHorizontalSpacing(6);
    gridLayout2->setVerticalSpacing(0);

    QMap<QString, QString> typeMap;
    typeMap.insert("大雪", ":/DaXue.png");
    typeMap.insert("晴", ":/Qing.png");
    typeMap.insert("小雨", ":/XiaoYu.png");
    typeMap.insert("雷阵雨", ":/LeiZhenYu.png");
    typeMap.insert("雪", ":/Xue.png");
    typeMap.insert("多云", ":/DuoYun.png");

    QList<QString> keys = typeMap.keys();

    for ( int i = 0; i < keys.size(); i++ )
    {
        QLabel* lblTypeIcon = new QLabel(this);
        QLabel* lblType = new QLabel(this);
        lblTypeIcon->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblType->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblTypeIcon->setPixmap(typeMap[keys[i]]);
        lblType->setText(keys[i]);
        lblTypeIcon->setStyleSheet(R"(
                border-bottom-left-radius: 0px;
                border-bottom-right-radius: 0px;
                padding-bottom: 2px;
                padding-left:20px;
                padding-right:20px;
                background-color: rgb(54, 93, 122);
            )");
        lblType->setStyleSheet(R"(
                border-top-left-radius: 0px;
                border-top-right-radius: 0px;
                padding-top: 2px;
                padding-left:20px;
                padding-right:20px;
                background-color: rgb(54, 93, 122);
            )");
        lblTypeIcon->setAlignment(Qt::AlignCenter);
        lblType->setAlignment(Qt::AlignCenter);

        mTypeList << lblType;
        mTypeIconList << lblTypeIcon;

        gridLayout2->addWidget(lblTypeIcon, 0, i);
        gridLayout2->addWidget(lblType, 1, i);
    }

    rightLayout->addLayout(gridLayout2);

    // 3. 空气质量
    QWidget* qualityWidget = new QWidget(this);
    qualityWidget->setStyleSheet("background-color:rgba(51,115,163,255);");
    qualityWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);  // 需要加这一行，否则缩放时，qualityWidget也会被缩放
    QHBoxLayout* hLayout = new QHBoxLayout(qualityWidget);
    hLayout->setSpacing(6);
    hLayout->setContentsMargins(0, 0, 0, 0);
    QStringList qualityList = {"优", "良", "轻度", "中度", "重度", "严重"};
    QStringList qualityBgList;
    qualityBgList << "rgb(121, 184, 0)"
                  << "rgb(255, 187, 23)"
                  << "rgb(255, 87, 97)"
                  << "rgb(235, 17, 27)"
                  << "rgb(170, 0, 0)"
                  << "rgb(110, 0, 0)";

    for ( int i = 0; i < qualityList.size(); i++ )
    {
        QLabel* lblQuality = new QLabel(this);
        lblQuality->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblQuality->setText(qualityList[i]);
        lblQuality->setStyleSheet(QString("padding:8px;background-color:%1;").arg(qualityBgList[i]));
        lblQuality->setAlignment(Qt::AlignCenter);

        mAqiList << lblQuality;

        hLayout->addWidget(lblQuality);
    }

    rightLayout->addWidget(qualityWidget);

    // 4. 温度曲线
    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout->setSpacing(0);

    lblHigh = new QLabel(this);
    lblHigh->setMinimumHeight(80);
    lblHigh->setStyleSheet(R"(
           border-bottom-left-radius: 0px;
           border-bottom-right-radius: 0px;
           padding-bottom: 2px;
           padding-left:20px;
           padding-right:20px;
           background-color: rgba(54, 93, 122,255);
       )");
    lblLow = new QLabel(this);
    lblLow->setMinimumHeight(80);
    lblLow->setStyleSheet(R"(
           border-top-left-radius: 0px;
           border-top-right-radius: 0px;
           padding-top: 2px;
           padding-left:20px;
           padding-right:20px;
           background-color: rgba(54, 93, 122,255);
       )");
    vLayout->addWidget(lblHigh);
    vLayout->addWidget(lblLow);
    rightLayout->addLayout(vLayout);

    // 5. 风力
    QGridLayout* gridLayout3 = new QGridLayout();
    gridLayout3->setHorizontalSpacing(6);
    gridLayout3->setVerticalSpacing(0);

    QStringList fxList = {"北风", "北风", "东风", "南风", "北风", "东北风"};
    QStringList flList = {"1级", "1级", "2级", "2级", "3级", "2级"};

    for ( int i = 0; i < fxList.size(); i++ )
    {
        QLabel* lblFx = new QLabel(this);
        QLabel* lblFl = new QLabel(this);
        lblFx->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblFl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        lblFx->setText(fxList[i]);
        lblFl->setText(flList[i]);
        lblFx->setStyleSheet(R"(
               border-bottom-left-radius: 0px;
               border-bottom-right-radius: 0px;
               padding-bottom: 2px;
               padding-left:20px;
               padding-right:20px;
               background-color: rgb(54, 93, 122);
           )");
        lblFl->setStyleSheet(R"(
               border-top-left-radius: 0px;
               border-top-right-radius: 0px;
               padding-top: 2px;
               padding-left:20px;
               padding-right:20px;
               background-color: rgb(54, 93, 122);
           )");
        lblFx->setAlignment(Qt::AlignCenter);
        lblFl->setAlignment(Qt::AlignCenter);

        mFxList << lblFx;
        mFlList << lblFl;

        gridLayout3->addWidget(lblFx, 0, i);
        gridLayout3->addWidget(lblFl, 1, i);
    }

    rightLayout->addLayout(gridLayout3);


    //9. 绘制高低温曲线
    lblHigh->installEventFilter(this);
    lblLow->installEventFilter(this);
}

void Widget::initData()
{
    // 1. 初始化天气类型，以及对应的图标
    mTypeMap.insert("暴雪", ":/BaoXue.png");
    mTypeMap.insert("暴雨", ":/BaoYu.png");
    mTypeMap.insert("暴雨到大暴雨", ":/BaoYuDaoDaBaoYu.png");
    mTypeMap.insert("大暴雨", ":/DaBaoYu.png");
    mTypeMap.insert("大暴雨到特大暴雨", ":/DaBaoYuDaoTeDaBaoYu.png");
    mTypeMap.insert("大到暴雪", ":/DaDaoBaoXue.png");
    mTypeMap.insert("大雪", ":/DaXue.png");
    mTypeMap.insert("大雨", ":/DaYu.png");
    mTypeMap.insert("冻雨", ":/DongYu.png");
    mTypeMap.insert("多云", ":/DuoYun.png");
    mTypeMap.insert("浮沉", ":/FuChen.png");
    mTypeMap.insert("雷阵雨", ":/LeiZhenYu.png");
    mTypeMap.insert("雷阵雨伴有冰雹", ":/LeiZhenYuBanYouBingBao.png");
    mTypeMap.insert("霾", ":/Mai.png");
    mTypeMap.insert("强沙尘暴", ":/QiangShaChenBao.png");
    mTypeMap.insert("晴", ":/Qing.png");
    mTypeMap.insert("沙尘暴", ":/ShaChenBao.png");
    mTypeMap.insert("特大暴雨", ":/TeDaBaoYu.png");
    mTypeMap.insert("undefined", ":/undefined.png");
    mTypeMap.insert("雾", ":/Wu.png");
    mTypeMap.insert("小到中雪", ":/XiaoDaoZhongXue.png");
    mTypeMap.insert("小到中雨", ":/XiaoDaoZhongYu.png");
    mTypeMap.insert("小雪", ":/XiaoXue.png");
    mTypeMap.insert("小雨", ":/XiaoYu.png");
    mTypeMap.insert("雪", ":/Xue.png");
    mTypeMap.insert("扬沙", ":/YangSha.png");
    mTypeMap.insert("阴", ":/Yin.png");
    mTypeMap.insert("雨", ":/Yu.png");
    mTypeMap.insert("雨夹雪", ":/YuJiaXue.png");
    mTypeMap.insert("阵雪", ":/ZhenXue.png");
    mTypeMap.insert("阵雨", ":/ZhenYu.png");
    mTypeMap.insert("中到大雪", ":/ZhongDaoDaXue.png");
    mTypeMap.insert("中到大雨", ":/ZhongDaoDaYu.png");
    mTypeMap.insert("中雪", ":/ZhongXue.png");
    mTypeMap.insert("中雨", ":/ZhongYu.png");

    // 2. 初始化4个城市天气信息
    // 哈尔滨
    WeatherInfo harbin;
    harbin.city = "哈尔滨";
    harbin.dateWeek = "2024/01/01 星期一";
    harbin.temp = -15;
    harbin.ganMao = "儿童、老年人及心脏呼吸系统疾病患者人群应减少长时间或高强度户外锻炼";
    harbin.pm25 = 76;
    harbin.humidity = 66;
    harbin.quality = "轻度";
    harbin.weekList = {"周日", "周一", "周二", "周三", "周四", "周五"};
    harbin.dateList = {"12/31", "01/01", "01/02", "01/03", "01/04", "01/05"};
    harbin.typeList = {"晴", "大雪", "大暴雨", "小雨", "小雪", "小雪"};
    harbin.qualityList = {23, 98, 150, 45, 98, 33};
    harbin.highTemp = {-12, -9, -15, -7, -3, -9};
    harbin.lowTemp = {-22, -28, -23, -25, -18, -22};
    harbin.fx = {"北风", "南风", "西南风", "南风", "南风", "西风"};
    harbin.fl = {2, 1, 1, 2, 3, 3};

    // 北京
    WeatherInfo beijing;
    beijing.city = "北京";
    beijing.dateWeek = "2024/01/01 星期一";
    beijing.temp = -2;
    beijing.ganMao = "儿童、老年人及心脏呼吸系统疾病患者人群应减少长时间或高强度户外锻炼";
    beijing.pm25 = 92;
    beijing.humidity = 55;
    beijing.quality = "轻度";
    beijing.weekList = {"周日", "周一", "周二", "周三", "周四", "周五"};
    beijing.dateList = {"12/31", "01/01", "01/02", "01/03", "01/04", "01/05"};
    beijing.typeList = {"晴", "晴", "多云", "晴", "多云", "小雨"};
    beijing.qualityList = {12, 45, 156, 88, 23, 9};
    beijing.highTemp = {2, 6, 12, 8, 5, 10};
    beijing.lowTemp = {-5, -9, -6, -12, -5, -1};
    beijing.fx = {"北风", "北风", "西北风", "西北风", "西北风", "西北风"};
    beijing.fl = {2, 1, 2, 2, 2, 2};

    // 上海
    WeatherInfo shanghai;
    shanghai.city = "上海";
    shanghai.dateWeek = "2024/01/01 星期一";
    shanghai.temp = 6;
    shanghai.ganMao = "极少数敏感人群应减少户外活动";
    shanghai.pm25 = 74;
    shanghai.humidity = 57;
    shanghai.quality = "良";
    shanghai.weekList = {"周日", "周一", "周二", "周三", "周四", "周五"};
    shanghai.dateList = {"12/31", "01/01", "01/02", "01/03", "01/04", "01/05"};
    shanghai.typeList = {"霾", "大暴雨", "小雪", "晴", "晴", "晴"};
    shanghai.qualityList = {12, 65, 12, 23, 34, 155};
    shanghai.highTemp = {10, 15, 12, 8, 11, 14};
    shanghai.lowTemp = {5, 8, 4, 12, 6, 11};
    shanghai.fx = {"北风", "北风", "东北风", "西北风", "东南风", "北风"};
    shanghai.fl = {3, 2, 2, 3, 2, 2};

    // 广州
    WeatherInfo guangzhou;
    guangzhou.city = "广州";
    guangzhou.dateWeek = "2024/01/01 星期一";
    guangzhou.temp = 23;
    guangzhou.ganMao = "极少数敏感人群应减少户外活动";
    guangzhou.pm25 = 47;
    guangzhou.humidity = 77;
    guangzhou.quality = "良";
    guangzhou.weekList = {"周日", "周一", "周二", "周三", "周四", "周五"};
    guangzhou.dateList = {"12/31", "01/01", "01/02", "01/03", "01/04", "01/05"};
    guangzhou.typeList = {"霾", "多云", "小雨", "晴", "晴", "晴"};
    guangzhou.qualityList = {23, 78, 33, 45, 66, 155};
    guangzhou.highTemp = {25, 27, 21, 18, 24, 20};
    guangzhou.lowTemp = {14, 11, 18, 9, 12, 16};
    guangzhou.fx = {"北风", "东风", "南风", "北风", "东风", "东南风"};
    guangzhou.fl = {1, 2, 2, 3, 2, 1};


    WeatherInfoList.append(harbin);
    WeatherInfoList.append(beijing);
    WeatherInfoList.append(shanghai);
    WeatherInfoList.append(guangzhou);
}

void Widget::updateUI()
{
    cityIndex++;

    if ( cityIndex > 3 )
    {
        cityIndex = 0;
    }

    WeatherInfo info = WeatherInfoList[cityIndex];

    // 1. 更新日期
    lblDate->setText(info.dateWeek);

    // 2. 更新天气类型、城市、高低温
    lblTypeIcon->setPixmap(mTypeMap[info.typeList[1]]);
    lblTemp->setText(QString::number(info.temp) + "°");
    lblCity->setText(info.city);
    lblType->setText(info.typeList[1]);
    lblLowHigh->setText(QString::number(info.lowTemp[1]) + "~" + QString::number(info.highTemp[1]) + "°C");

    lblGanMao->setText("感冒指数：" + info.ganMao);

    lblFx->setText(info.fx[1]);
    lblFl->setText(QString::number(info.fl[1]) + "级");

    lblPM25->setText(QString::number(info.pm25));

    lblHumidity->setText(QString::number(info.humidity) + "%");

    lblQuality->setText(QString::number(info.qualityList[1]));

    // 3. 更新六天
    for ( int i = 0; i < 6; i++ )
    {
        // 3.1 更新星期和日期
        mWeekList[i]->setText(info.weekList[i]);

        //设置 昨天/今天/明天 的星期显示 - 不显示星期几，而是显示
        //“昨天”、“今天”、“明天”
        mWeekList[0]->setText("昨天");
        mWeekList[1]->setText("今天");
        mWeekList[2]->setText("明天");

        mDateList[i]->setText(info.dateList[i]);

        // 3.2 更新天气类型
        mTypeIconList[i]->setPixmap(mTypeMap[info.typeList[i]]);
        mTypeList[i]->setText(info.typeList[i]);

        // 3.3 更新空气质量
        if ( info.qualityList[i] >= 0 && info.qualityList[i] <= 50 )
        {
            mAqiList[i]->setText("优");
            mAqiList[i]->setStyleSheet("background-color: rgb(121, 184, 0);");
        }
        else if ( info.qualityList[i] >= 50 && info.qualityList[i] <= 100 )
        {
            mAqiList[i]->setText("良");
            mAqiList[i]->setStyleSheet("background-color: rgb(255, 187, 23);");
        }
        else if ( info.qualityList[i] >= 100 && info.qualityList[i] <= 150 )
        {
            mAqiList[i]->setText("轻度");
            mAqiList[i]->setStyleSheet("background-color: rgb(255, 87, 97);");
        }
        else if ( info.qualityList[i] >= 150 && info.qualityList[i] <= 200 )
        {
            mAqiList[i]->setText("中度");
            mAqiList[i]->setStyleSheet("background-color: rgb(235, 17, 27);");
        }
        else if ( info.qualityList[i] >= 200 && info.qualityList[i] <= 300 )
        {
            mAqiList[i]->setText("重度");
            mAqiList[i]->setStyleSheet("background-color: rgb(170, 0, 0);");
        }
        else
        {
            mAqiList[i]->setText("严重");
            mAqiList[i]->setStyleSheet("background-color: rgb(110, 0, 0);");
        }

        // 3.4 更新风力/风向
        mFxList[i]->setText(info.fx[i]);
        mFlList[i]->setText(QString::number(info.fl[i]) + "级");
    }

    // 4. 绘制高低温曲线
    lblHigh->update();
    lblLow->update();
}

void Widget::paintHighCurve()
{
    WeatherInfo info = WeatherInfoList[cityIndex];

    QPainter painter(lblHigh);

    // 抗锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 1. 获取 x 轴坐标
    int pointX[6] = {0};

    for ( int i = 0; i < 6; i++ )
    {
        pointX[i] = mAqiList[i]->pos().x() + mAqiList[i]->width() / 2;
    }

    // 2. 获取 y 轴坐标

    //  int temp[6]   = {0};
    int tempSum = 0;
    int tempAverage = 0;

    // 2.1 计算平均值
    for ( int i = 0; i < 6; i++ )
    {
        tempSum += info.highTemp[i];
    }

    tempAverage = tempSum / 6;  // 最高温平均值

    //    qDebug() << "paintHighCurve" << tempAverage;

    // 2.2 计算 y 轴坐标
    int pointY[6] = {0};
    int yCenter = lblHigh->height() / 2;

    for ( int i = 0; i < 6; i++ )
    {
        pointY[i] = yCenter - ((info.highTemp[i] - tempAverage) * INCREMENT);
    }

    // 3. 开始绘制
    // 3.1 初始化画笔
    QPen pen = painter.pen();
    pen.setWidth(1);                    //设置画笔宽度为1
    pen.setColor(QColor(255, 170, 0));  //设置颜色
    painter.save();

    painter.setPen(pen);
    painter.setBrush(QColor(255, 170, 0));  //设置画刷颜色

    // 3.2 画点、写文本
    for ( int i = 0; i < 6; i++ )
    {
        painter.drawEllipse(QPoint(pointX[i], pointY[i]), POINT_RADIUS, POINT_RADIUS);
        painter.drawText(QPoint(pointX[i] - TEXT_OFFSET_X, pointY[i] - TEXT_OFFSET_Y), QString::number(info.highTemp[i]) + "°");
    }

    // 3.3 绘制曲线
    for ( int i = 0; i < 5; i++ )
    {
        if ( i == 0 )
        {
            pen.setStyle(Qt::DotLine);  //虚线
            painter.setPen(pen);
        }
        else
        {
            pen.setStyle(Qt::SolidLine);  // 实线
            painter.setPen(pen);
        }

        painter.drawLine(pointX[i], pointY[i], pointX[i + 1], pointY[i + 1]);
    }

    painter.restore();
}

void Widget::paintLowCurve()
{
    WeatherInfo info = WeatherInfoList[cityIndex];

    QPainter painter(lblLow);

    // 抗锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 1. 获取 x 轴坐标
    int pointX[6] = {0};

    for ( int i = 0; i < 6; i++ )
    {
        pointX[i] = mAqiList[i]->pos().x() + mAqiList[i]->width() / 2;
    }

    // 2. 获取 y 轴坐标

    //  int temp[6]   = {0};
    int tempSum = 0;
    int tempAverage = 0;

    // 2.1 计算平均值
    for ( int i = 0; i < 6; i++ )
    {
        tempSum += info.lowTemp[i];
    }

    tempAverage = tempSum / 6;  // 最高温平均值

    //    qDebug() << "paintLowCurve" << tempAverage;

    // 2.2 计算 y 轴坐标
    int pointY[6] = {0};
    int yCenter = lblLow->height() / 2;

    for ( int i = 0; i < 6; i++ )
    {
        pointY[i] = yCenter - ((info.lowTemp[i] - tempAverage) * INCREMENT);
    }

    // 3. 开始绘制
    // 3.1 初始化画笔
    QPen pen = painter.pen();
    pen.setWidth(1);                    //设置画笔宽度为1
    pen.setColor(QColor(0, 255, 255));  //设置颜色
    painter.save();

    painter.setPen(pen);
    painter.setBrush(QColor(0, 255, 255));  //设置画刷颜色

    // 3.2 画点、写文本
    for ( int i = 0; i < 6; i++ )
    {
        painter.drawEllipse(QPoint(pointX[i], pointY[i]), POINT_RADIUS, POINT_RADIUS);
        painter.drawText(QPoint(pointX[i] - TEXT_OFFSET_X, pointY[i] - TEXT_OFFSET_Y), QString::number(info.lowTemp[i]) + "°");
    }

    // 3.3 绘制曲线
    for ( int i = 0; i < 5; i++ )
    {
        if ( i == 0 )
        {
            pen.setStyle(Qt::DotLine);  //虚线
            painter.setPen(pen);
        }
        else
        {
            pen.setStyle(Qt::SolidLine);  // 实线
            painter.setPen(pen);
        }

        painter.drawLine(pointX[i], pointY[i], pointX[i + 1], pointY[i + 1]);
    }

    painter.restore();
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    // 无边框
    setWindowFlag(Qt::FramelessWindowHint);

    this->setObjectName("weather_widget");
    this->setStyleSheet(R"(
                        QWidget#weather_widget {
                            background-color:rgba(50,115,165,255);
                        }
                        QLabel {
                            font: 12pt "Microsoft YaHei";
                            border-radius: 4px;
                            color: rgb(255, 255, 255);
                            padding: 12px;
                        }
                    )");

    //1 创建主体布局
    mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(10);
    mainLayout->setContentsMargins(18, 18, 18, 18);

    //2  顶部 布局
    topLayout = new QHBoxLayout();


    //3  底部布局
    QHBoxLayout* bottonLayout = new QHBoxLayout();
    leftLayout = new QVBoxLayout(this);
    leftLayout->setContentsMargins(0, 0, 0, 0);

    rightLayout = new QVBoxLayout(this);
    rightLayout->setSpacing(16);//设置一个间距

    bottonLayout->addLayout(leftLayout);
    bottonLayout->addLayout(rightLayout);
    //设置拉伸比例
    bottonLayout->setStretch(0, 2);
    bottonLayout->setStretch(1, 4);

    //4 将顶部和底部布局,添加到主布局
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bottonLayout);


    initTop();

    initLeft();

    initRight();

    initData();

    //右键菜单,退出
    mExitMenu = new QMenu(this);
    mExitAct = new QAction();
    mExitAct->setText("退出");
    mExitAct->setIcon(QIcon(":/close.png"));
    mExitMenu->addAction(mExitAct);

    connect(mExitAct, &QAction::triggered, this, [ = ]()
    {
        qApp->exit(0);
    });


    //创建并启动定时器-每隔 3 秒切换城市
    QTimer* timer = new QTimer();
    connect(timer, &QTimer::timeout, this, &Widget::updateUI);
    cityIndex = 0;
    timer->start(2000);

}

Widget::~Widget()
{
}

